package com.zch.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zch.entity.Result;
import com.zch.entity.pojo.Edit;
import com.zch.entity.utils.DuanxinUtil;
import com.zch.mapper.EditMapper;
import com.zch.service.EditService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class EditServiceImpl extends ServiceImpl<EditMapper, Edit> implements  EditService {
    @Autowired
    public StringRedisTemplate redisTemplate;
    @Override
    public Result sendSms(String phone) {
        //1. 生成6位验证码
        String code = RandomUtil.randomNumbers(6);
        //2. 将验证码发送到手机上
        Boolean flag = DuanxinUtil.sendDuanxin(phone, code);
        //3. 发送失败重新发送
        if (flag==false){
            DuanxinUtil.sendDuanxin(phone, code);


            System.out.println("需要此代码");

            return Result.error("验证码发送失败666111");
        }
        //4.存储到redis中
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        checkToken(phone);
        return null;
    }

    /**
     * 判断手机号
     * @param phone
     * @return
     */
    private Result<?> checkToken(String phone){
        if (StringUtils.isNotEmpty(phone)){
            return Result.error("不能为空");
        }
        return Result.ok();
    }
 }
